package cn.hx.mall_goods.controller;


import cn.hx.mall_goods.entity.HomeNewProduct;
import cn.hx.mall_goods.entity.Product;
import cn.hx.mall_goods.service.HomeNewProductService;
import cn.hx.mall_goods.service.ProductCategoryService;
import cn.hx.mall_goods.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ProductController {

    @Resource
    private ProductService productService;
    @Resource
    private HomeNewProductService homeNewProductService;

    @GetMapping("/recommendedProducts")
    public List<Product> product() {
        Page<Product> page = new Page<>(0, 8);
        Page<Product> page1 = productService.page(page, null);
        System.out.println(page1.getRecords());
        return page1.getRecords();
    }

    @GetMapping("/similarGoods/{id}")
    public List<Product> product(@PathVariable("id") int id) {
        Page<Product> page = new Page<>(0, 3);
        Page<Product> page1 = productService.page(page, new QueryWrapper<Product>().eq("product_category_id",id));
        System.out.println(page1.getRecords());
        return page1.getRecords();
    }

    @GetMapping("/newProducts")
    public List<Product> getNewProduct(){
        List<Product> list = new ArrayList<>();
        List<HomeNewProduct> recommend_status = homeNewProductService.list(
                new QueryWrapper<HomeNewProduct>().eq("recommend_status", "1"));
        for (HomeNewProduct recommendStatus : recommend_status) {
            list.add(productService.getOne(new QueryWrapper<Product>().eq("id",recommendStatus.getProductId())));
        }
        return list;
    }
}
